package com.trkj.controller.hjj;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.trkj.pojo.StaffInformation;
import com.trkj.service.ServiceException;
import com.trkj.service.hjj.TimeCardModuleService;
import com.trkj.vo.Result_AddRule_Result;

@Controller
public class TimeCardModuleController {
	@Autowired
	private TimeCardModuleService timeCardModuleService;//考勤卡部门业务
	/**\
	 * 模糊搜索员工
	 * @param page
	 * @param rows
	 * @param stainName
	 * @return
	 */
	@RequestMapping("is_to_all_emp_mohu")
	@ResponseBody
	public Map findEmpMOHU(int page,int rows,String stainName){
		Page<JSONArray> pages = PageHelper.startPage(page, rows);
		timeCardModuleService.findEmpMOHU(stainName);
		Map map = new HashMap<>();
		map.put("total", pages.getTotal());
		map.put("rows", pages.getResult());
		return map;
	}
	/**
	 * 查询有员工的部门
	 * @return
	 */
	@RequestMapping("is_to_dept_emp_alls")
	@ResponseBody
	public Map findDeptFindEmp(int page,int rows,String dName){
		Page<JSONArray> pages = PageHelper.startPage(page, rows);
		timeCardModuleService.findDeptFindEmp(dName);
		Map map = new HashMap<>();
		map.put("total", pages.getTotal());
		map.put("rows", pages.getResult());
		return map;
	}
	/**
	 * 查询所有员工
	 * @return
	 */
	@RequestMapping("is_to_all_depts")
	@ResponseBody
	public JSONArray find_All_Dept(){
		return timeCardModuleService.find_All_Dept();
	}
	/**
	 * 换卡
	 * @param stainId
	 * @param timecardId
	 * @param timecardId1
	 * @param attcardId
	 * @param exchageTime
	 * @return
	 */
	@RequestMapping("is_exchange_timecard")
	@ResponseBody
	public Result_AddRule_Result isExchangeTimecard(Long stainId,Long timecardId,Long timecardId1,Long attcardId,Timestamp exchageTime){
		try {
			boolean b = timeCardModuleService.isExchangeTimecard(stainId, timecardId, timecardId1, attcardId, exchageTime);
			return Result_AddRule_Result.SUCCESS;
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return Result_AddRule_Result.failure(e.getMessage());
		}
	}
	/**
	 * 查询所有的员工考勤卡
	 * @return
	 */
	@RequestMapping("to_is_findTimecardandEmp")
	@ResponseBody
	public Map findTimeCardandEmp(int page,int rows){
		Page<JSONArray> pages = PageHelper.startPage(page, rows);
		timeCardModuleService.findTimeCardandEmp();
		Map map = new HashMap<>();
		map.put("total", pages.getTotal());
		map.put("rows", pages.getResult());
		return map;
	}
	/**
	 * 打开考勤卡
	 * @param timecardId
	 * @param timecardZt
	 * @return
	 */
	@RequestMapping("is_open_timecard")
	@ResponseBody
	public Result_AddRule_Result openTimecardById(Long timecardId,Long timecardZt){
		try {
			boolean b = timeCardModuleService.openTimecard(timecardId, timecardZt);
			return Result_AddRule_Result.SUCCESS;
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return Result_AddRule_Result.failure(e.getMessage());
		}
	}
	/**
	 * 查询考勤卡 停用
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping("is_to_findTimecardBystaeOne")
	@ResponseBody
	public Map findTimecardBystaeOne(int page,int rows){
		Page<JSONArray> pages = PageHelper.startPage(page, rows);
		timeCardModuleService.findTimecardBystaeOne();
		Map map = new HashMap<>();
		map.put("total", pages.getTotal());
		map.put("rows", pages.getResult());
		return map;
	}
	/**
	 * 查询未分配的考勤卡按照id
	 * @param page
	 * @param rows
	 * @param timecardId
	 * @return
	 */
	@RequestMapping("find_timecard_id_no")
	@ResponseBody
	public Map findTimeCardBytimecardId(int page,int rows,Integer timecardId){
		Page<JSONArray> pages = PageHelper.startPage(page, rows);
		timeCardModuleService.findTimeCardBytimecardId(timecardId);
		Map map = new HashMap<>();
		map.put("total", pages.getTotal());
		map.put("rows", pages.getResult());
		return map;
	}
	/**
	 * 分配考勤卡
	 * @param timecardId
	 * @param stainId
	 * @param timecardDate
	 * @return
	 */
	@RequestMapping("fenpei_timecard")
	@ResponseBody
	public Result_AddRule_Result fenpeiTimecard(Long timecardId,Long stainId,Timestamp timecardDate,HttpSession session){
		try {
			StaffInformation sta = (StaffInformation) session.getAttribute("staff");
			boolean b = timeCardModuleService.fenpeiTimecard(timecardId, stainId, timecardDate,sta.getStainName());
			return Result_AddRule_Result.SUCCESS;
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return Result_AddRule_Result.failure(e.getMessage());
		}
	}
	@RequestMapping("to_is_timecardStateZero")
	@ResponseBody
	public Map findTimecardStateZero(int page,int rows){
		Page<JSONArray> pages = PageHelper.startPage(page, rows);
		timeCardModuleService.findTimecardStateZero();
		Map map = new HashMap<>();
		map.put("total", pages.getTotal());
		map.put("rows", pages.getResult());
		return map;
	}
	/**
	 * 删除考勤配卡
	 * @param attcardId
	 * @param timecardId
	 * @return
	 */
	@RequestMapping("del_timecardByid")
	@ResponseBody
	public Result_AddRule_Result delTimecardById(Long attcardId,Long timecardId){
		try {
			timeCardModuleService.delTimecardWhih(attcardId, timecardId);
		
			return Result_AddRule_Result.SUCCESS;
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return Result_AddRule_Result.failure(e.getMessage());
		}
	}
	/**
	 * 根据考勤卡号查询考勤配卡
	 * @param page
	 * @param rows
	 * @param timecardId
	 * @return
	 */
	@RequestMapping("to_is_all_findTimecardwhithBytimecardId")
	@ResponseBody
	public Map FindTimecardwhithBytimecardId(int page,int rows,Integer timecardId){
		Page<JSONArray> pages = PageHelper.startPage(page, rows);
		timeCardModuleService.FindTimecardwhithBytimecardId(timecardId);
		Map map = new HashMap<>();
		map.put("total", pages.getTotal());
		map.put("rows", pages.getResult());
		return map;
	}
	/**
	 * 查询所有的考勤卡
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping("is_to_findAllAttendancecard")
	@ResponseBody
	public Map findAllAttendancecard(int page,int rows){
		Page<JSONArray> pages = PageHelper.startPage(page, rows);
		timeCardModuleService.findAllAttendancecard();
		Map map = new HashMap<>();
		map.put("total", pages.getTotal());
		map.put("rows", pages.getResult());
		return map;
	}
	/**
	 * 更据编号查询考勤卡
	 * @param page
	 * @param rows
	 * @param timecardId
	 * @return
	 */
	@RequestMapping("to_is_all_timecard_byId")
	@ResponseBody
	public Map findAllTimecardById(int page,int rows,Integer timecardId){
		Page<JSONArray> pages = PageHelper.startPage(page, rows);
		timeCardModuleService.findAlltiemCard(timecardId);
		Map map = new HashMap<>();
		map.put("total", pages.getTotal());
		map.put("rows",pages.getResult());
		return map;
	}
	/**
	 * 新增考勤配卡
	 * @param stainId
	 * @param timecardId
	 * @param attcardEffect
	 * @param attcardLoseeff
	 * @param attcardReason
	 * @param attcardRemarks
	 * @return
	 */
	@RequestMapping("is_add_timecard_whith")
	@ResponseBody
	public Result_AddRule_Result inserTimecard_whith(Long stainId,Long timecardId,Timestamp attcardEffect,Timestamp attcardLoseeff,String attcardReason,String attcardRemarks,Timestamp attcardStartdate,HttpSession session){
		StaffInformation staff= (StaffInformation) session.getAttribute("staff");
		
		boolean b =timeCardModuleService.inserAttendancecard(stainId, timecardId, attcardEffect, attcardLoseeff, attcardReason, attcardRemarks,attcardStartdate,staff.getStainName());
		try {
			return Result_AddRule_Result.SUCCESS;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return Result_AddRule_Result.failure(e.getMessage());
		}
	}
	/**
	 * 查询所有的员工和部门
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping("is_to_all_staffanddept")
	@ResponseBody
	public Map findAllStaffDept(int page,int rows){
		Page<JSONArray> pages = PageHelper.startPage(page, rows);
		timeCardModuleService.findAllStaffDept();
		Map map = new HashMap<>();
		map.put("total", pages.getTotal());
		map.put("rows",pages.getResult());
		return map;
	}
	/**
	 * 查询所有的考勤卡
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping("is_to_all_timecards")
	@ResponseBody
	public Map findAllTimecard(int page,int rows){
		Page<JSONArray> pages = PageHelper.startPage(page, rows);
		timeCardModuleService.findAllTimecardStaff();
		Map map = new HashMap<>();
		map.put("total", pages.getTotal());
		map.put("rows", pages.getResult());
		return map;
	}
}
